var status;
var pointsId = 167200;
var winPoint = 5;//任務全部通過獎勵的段位點。
var p;
/*
 * 战斗力5的渣渣：10点
 青铜Ⅴ：30点
 青铜Ⅳ
 青铜Ⅲ
 青铜Ⅱ
 黄金：80点
 白金：100点
 钻石：120点
 超凡大师：180点
 最强王者：200点
 */

function start() {
    status = -1;
    action(1, 0, 0);
}

function action(mode, type, selection) {
    if (mode == 0) {
        cm.dispose();
        return;
    } else if (mode == 1) {
        status++;
    } else {
        status--;
    }

    if (status == 0) {
        p = new Points();
        var points = p.getEventPoints(pointsId, cm.getPlayer().getId());//目前積分
        var Ranking = RankingName(points);
		cm.givePartyEnergy(1500);
		cm.givePartyPoints(1500);
		cm.gainItem(2614030, 3);
		cm.gainItem(5062000, 3);	
		cm.gainItem(5062002, 3);
		cm.gainItem(5062009, 3);
		cm.gainItem(2431347, 3);
		cm.gainItem(2431114, 3);
		cm.gainItem(2436249, 3);		
		cm.gainNX(2, 1500);
        cm.sendNext("#b段位：#r" + Ranking + "（" + points + "） #b分。\r\n完成任务获取点数 + #r" + winPoint + "#b。\r\n 您获得的组队任务奖品：\r\n#v2614046#x4 #v5062000#x5 #v5062002#x5 #v2431347#x3 #v2431114#x3 #v2436249#x3 \r\n抵用券x1500 PPMS活跃x1500 PPMS微积分x1500現在傳送出去。");
    } else if (status == 1) {
        p.setEventPoints(pointsId, cm.getPlayer().getId(), winPoint);
        var points = p.getEventPoints(pointsId, cm.getPlayer().getId());//目前積分
        var Ranking = RankingName(points);
        cm.warp(940020000, 0);

        cm.sendOk("- #e#d完成任务#n#k\r\n\r\n#b获得了 #r" + winPoint + " #b段位点。\r\n目前情況：\r\n段位：#r" + Ranking + "（" + points + "） #b分。");
        cm.dispose();
    }
}

function RankingName(points) {
    if (points >= 0 && points <= 10) {
        return "战斗力5的渣渣";
    }
    if (points >= 11 && points <= 30) {
        return "青铜Ⅴ";
    }
    if (points >= 31 && points <= 50) {
        return "青铜Ⅳ";
    }
    if (points >= 51 && points <= 80) {
        return "青铜Ⅲ";
    }
    if (points >= 81 && points <= 100) {
        return "青铜Ⅱ";
    }
    if (points >= 101 && points <= 120) {
        return "青铜Ⅰ";
    }
    if (points >= 121 && points <= 140) {
        return "白银Ⅴ";
    }
	if (points >= 141 && points <= 160) {
        return "白银Ⅳ";
    }
	if (points >= 161 && points <= 180) {
        return "白银Ⅲ";
    }
	if (points >= 181 && points <= 200) {
        return "白银Ⅱ";
    }
	if (points >= 201 && points <= 220) {
        return "白银Ⅰ";
    }
	if (points >= 231 && points <= 260) {
        return "黄金Ⅴ";
    }
	if (points >= 261 && points <= 290) {
        return "黄金Ⅳ";
    }
	if (points >= 291 && points <= 320) {
        return "黄金Ⅲ";
    }
	if (points >= 321 && points <= 350) {
        return "黄金Ⅱ";
    }
	if (points >= 351 && points <= 400) {
        return "黄金Ⅰ";
    }
	if (points >= 401 && points <= 450) {
        return "白金Ⅴ";
    }
	if (points >= 451 && points <= 500) {
        return "白金Ⅳ";
    }
	if (points >= 501 && points <= 550) {
        return "白金Ⅲ";
    }
	if (points >= 551 && points <= 600) {
        return "白金Ⅱ";
    }
	if (points >= 601 && points <= 650) {
        return "白金Ⅰ";
    }
	if (points >= 651 && points <= 700) {
        return "钻石Ⅴ";
    }
	if (points >= 701 && points <= 800) {
        return "钻石Ⅳ";
    }
	if (points >= 801 && points <= 900) {
        return "钻石Ⅲ";
    }
	if (points >= 901 && points <= 1000) {
        return "钻石Ⅱ";
    }
	if (points >= 1001 && points <= 1100) {
        return "钻石Ⅰ";
    }
	if (points >= 1101 && points <= 1499) {
        return "超凡大师";
    }
    if (points >= 1500) {
        return "最強王者荣耀";
    }
}



var Points = function() {
    this.DelEventPoints = function(Eventid, charid) {
        var delectData = cm.getConnection().prepareStatement("delete from EventTimes where eventid = " + Eventid + " and cid = " + charid + "");
        delectData.executeUpdate(); //删除数据
    }

    this.DelEventPoints = function(Eventid, charid) {
        var delectData = cm.getConnection().prepareStatement("delete from EventTimes where eventid = " + Eventid + " and cid = " + charid + "");
        delectData.executeUpdate(); //删除数据
    }

    this.getEventTimes = function(Eventid, charid) {//通过eventid来得到参与这个活动的次数
        var i = 0;
        var Times = cm.getConnection().prepareStatement("SELECT * FROM EventTimes where eventid = " + Eventid + " and cid = " + charid + "").executeQuery(); // 查询数据
        while (Times.next()) {
            i = Times.getString("times");//得到次数
        }
        return parseInt(i);
    }

    this.getEventPoints = function(Eventid, charid) {//通过eventid来得到参与这个活动的点数
        var i = 0;
        var Times = cm.getConnection().prepareStatement("SELECT * FROM EventTimes where eventid = " + Eventid + " and cid = " + charid + "").executeQuery(); // 查询数据
        while (Times.next()) {
            i = Times.getString("points");//得到点数
        }
        return parseInt(i);
    }

    this.setEventPoints = function(Eventid, charid, points) {//通过eventid来给予参与这个活动的点数
        var i = 0;
        var Times = cm.getConnection().prepareStatement("SELECT * FROM EventTimes where eventid = " + Eventid + " and cid = " + charid + "").executeQuery(); // 查询数据
        while (Times.next()) {
            i++;
        }
        if (i == 0) {//insert
            var insert = cm.getConnection().prepareStatement("INSERT INTO EventTimes VALUES(?,?,?,?,?,?,?)"); // 载入数据
            insert.setString(1, null); //载入记录ID
            insert.setString(2, Eventid); //载入活动ID
            insert.setString(3, cm.getPlayer().getId());//cid
            insert.setString(4, cm.getPlayer().getName());//cname
            insert.setString(5, points);//points 点数
            insert.setString(6, this.getEventTimes(1, charid));//times 次数
            insert.setString(7, null);//
            insert.executeUpdate(); //更新
        } else {//update
            var update = cm.getConnection().prepareStatement("update EventTimes set points = ? where eventid = " + Eventid + " and cid = " + charid + "");//更新为已使用
            update.setString(1, this.getEventPoints(Eventid, charid) + points);
            update.executeUpdate();
        }
    }

    this.setEventTimes = function(Eventid, charid, times) {//通过eventid来设置参与这个活动的次数
        var i = 0;
        var Times = cm.getConnection().prepareStatement("SELECT * FROM EventTimes where eventid = " + Eventid + " and cid = " + charid + "").executeQuery(); // 查询数据
        while (Times.next()) {
            i++;
        }
        if (i == 0) {//insert
            var insert = cm.getConnection().prepareStatement("INSERT INTO EventTimes VALUES(?,?,?,?,?,?,?)"); // 载入数据
            insert.setString(1, null); //载入记录ID
            insert.setString(2, Eventid); //载入活动ID
            insert.setString(3, cm.getPlayer().getId());//cid
            insert.setString(4, cm.getPlayer().getName());//cname
            insert.setString(5, this.getEventPoints(2, charid));//points 点数
            insert.setString(6, times);//times 次数
            insert.setString(7, null);//
            insert.executeUpdate(); //更新
        } else {//update
            var update = cm.getConnection().prepareStatement("update EventTimes set times = ? where eventid = " + Eventid + " and cid = " + charid + "");//更新为已使用
            update.setString(1, this.getEventTimes(Eventid, charid) + times);
            update.executeUpdate();
        }
    }
}
